package com.fzkj.nio.part2;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * 客户端实现案例：基于nio非阻塞通信
 */
public class Client {

    public static void main(String[] args) throws Exception {
        // 1、获取通信
        SocketChannel sChannel = SocketChannel.open(
                new InetSocketAddress("127.0.0.1", 2323));
        // 2、切换为非阻塞模式
        sChannel.configureBlocking(false);
        // 3、分配缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        // 4、发送数据
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.print("要发送的数据：");
            String msg = sc.nextLine();
            buffer.put(("我："+msg).getBytes());
            buffer.flip();
            sChannel.write(buffer);
            buffer.clear();
        }

    }

}
